//
//  SSKeyChainMgr.m
//  DoulinClient
//
//  Created by XP on 2018/12/5.
//

#import "SSKeyChainMgr.h"
#import "SSKeychain.h"

//static NSString * kKCSer = @"com.lxkj.intelligentCommunity";
  static NSString * kKCSer = @"9F2BA010252C41CA";


@implementation SSKeyChainMgr

/** uuid */
+ (NSString *)getUUID {
    NSString * uuid = [SSKeychain passwordForService:kKCSer account:@"myUuid"];
    if ((uuid == nil) || (uuid.length == 0) || [uuid isKindOfClass:NSNull.class]) {
        NSString * uuid1 = [self creatUUID];
        BOOL flag = [SSKeychain setPassword:uuid1 forService:kKCSer account:@"myUuid"];
        if (flag){
            NSLog(@"<<-- uuid 保存成功🔥:%@ -->>", uuid1);
        } else {
            NSLog(@"<<-- uuid 保存失败🔥:%@ -->>", uuid1);
        }
        uuid = uuid1;
    }
    return uuid;
}
+ (BOOL)delUUID {
    return [SSKeychain deletePasswordForService:kKCSer account:@"myUuid"];
}
+ (NSString *)creatUUID{
    CFUUIDRef puuid = CFUUIDCreate(nil);
    CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
    NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));
    CFRelease(puuid);
    CFRelease(uuidString);
    NSLog(@"<<-- %@ -->>", result);
    return result;
}
/** AES的Key:16位 */
+ (NSString *)getAESKey{
    NSString * aesKey = [SSKeychain passwordForService:kKCSer account:@"aesKey"];
    if ((aesKey == nil) || (aesKey.length == 0) || [aesKey isKindOfClass:NSNull.class]) {
        NSString * uuid = [self getUUID];
        NSString * key = [[uuid stringByReplacingOccurrencesOfString:@"-" withString:@""] substringToIndex:16];
        BOOL flag = [SSKeychain setPassword:key forService:kKCSer account:@"aesKey"];
        if (flag) {
            NSLog(@"<<-- aesKey 保存成功🔥:%@ -->>", key);
        } else {
            NSLog(@"<<-- aesKey 保存失败🔥:%@ -->>", key);
        }
        aesKey = key;
    }
    return aesKey;
}
+ (BOOL)delAESKey {
    return [SSKeychain deletePasswordForService:kKCSer account:@"aesKey"];
}
/** RSA的公钥 */
+ (void)saveRSAKey:(NSString *)key {
    BOOL flag = [SSKeychain setPassword:key forService:kKCSer account:@"rsaKey"];
    if (flag) {
        NSLog(@"<<-- rsaKey 保存成功🔥:%@ -->>", key);
    } else {
        NSLog(@"<<-- rsaKey 保存失败🔥:%@ -->>", key);
    }
}
+ (NSString *)getRSAKey {
    return [SSKeychain passwordForService:kKCSer account:@"rsaKey"];
}
+ (BOOL)delRSAKey {
    return [SSKeychain deletePasswordForService:kKCSer account:@"rsaKey"];
}
/** 验证登录的token */
+ (void)saveLoginToken:(NSString *)token {
    BOOL flag = [SSKeychain setPassword:token forService:kKCSer account:@"loginToken"];
    if (flag) {
        NSLog(@"<<-- loginToken 保存成功🔥:%@ -->>", token);
    } else {
        NSLog(@"<<-- loginToken 保存失败🔥:%@ -->>", token);
    }
}
+ (NSString *)getLoginToken{
    return [SSKeychain passwordForService:kKCSer account:@"loginToken"];
}
+ (BOOL)delLoginToken {
    return [SSKeychain deletePasswordForService:kKCSer account:@"loginToken"];
}

@end
